//
//  LXCooperationtApplyViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/24.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXCooperationtApplyViewController.h"
#import "LXTableViewCell.h"
#import "LXChooseCarViewViewController.h"
#import "LXApplyAnnexTableCell.h"
#import "LXChooseBtnTableViewCell.h"
#import "LXRepairModel.h"
#import "LXRemarkTabelViewCell.h"
#import "LXPhotoHelper.h"
#import "LXChooseManagerViewController.h"
#import "FuZeRenModel.h"
#import "KeHuInfoModel.h"
#import "LXChooseCustomViewController.h"
#import "LXCooperationListViewController.h"
@interface LXCooperationtApplyViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSArray *data;
@property (nonatomic) NSMutableDictionary *loanDic;

@end

@implementation LXCooperationtApplyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.type == 1) {
        [self layoutCustomNav:self.view title:@"添加修理厂"];
    }
    else {
        [self layoutCustomNav:self.view title:@"新增合作公司"];
    }
    
    
 
    [self.tableView registerClass:[LXTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LXTableViewCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXRemarkTabelViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];

    self.view.backgroundColor = White_Color;
    [self.view addSubview:self.tableView];
    
    [self loadData];
    
    [self setBottomViewWithText:@"确认添加"];
    
}
- (void)loadData{
    
    self.loanDic  = [NSMutableDictionary dictionary];
    
    
    self.data = @[
         
           @{
               @"title":self.type==1?@"*修理厂名称":@"*合作公司名称",
               @"key":@"name",
               @"placeHolder" : self.type==1?@"请输入修理厂名称":@"请输入合作公司名称",
               @"canEdit":@(YES),
               @"showAccess":@(NO),
               @"maxLength":@(1000),
               @"sel":@"",
               @"height":@(kComHeight)
           },
           @{
               @"title":self.type==1?@"*修理厂地址":@"*合作公司地址",
               @"key":@"address",
               @"placeHolder" : self.type==1?@"请输入修理厂地址":@"请输入合作公司地址",
               @"canEdit":@(YES),
               @"showAccess":@(NO),
               @"maxLength":@(1000),
               @"sel":@"",
               @"height":@(kComHeight)
           },
           @{
               @"title":@"*联系人姓名",
               @"key":@"contact",
               @"placeHolder" : @"请输入联系人姓名",
               @"canEdit":@(YES),
               @"showAccess":@(NO),
               @"maxLength":@(1000),
               @"inputType":@(CHINESE),
               @"sel":@"",
               @"height":@(kComHeight)
           },
           @{
               @"title":@"*联系人手机号",
               @"key":@"contactPhone",
               @"placeHolder" : @"请输入联系人手机号",
               @"canEdit":@(YES),
               @"showAccess":@(NO),
               @"maxLength":@(11),
               @"keyboardType":@(UIKeyboardTypeNumberPad),
               @"inputType":@(NUM),
               @"sel":@"",
               @"height":@(kComHeight)
           },
           @{
               @"title":@"*联系人电话",
               @"key":@"tel",
               @"placeHolder" : @"请输入联系人电话",
               @"canEdit":self.type==1?@(YES):@(NO),
               @"showAccess":@(NO),
               @"maxLength":@(11),
               @"keyboardType":@(UIKeyboardTypeNumberPad),
               @"inputType":@(NUM),
               @"sel":@"",
               @"height":self.type==1?@(kComHeight):@(0.00001)
           },
           @{
               @"title":@"开户行",
               @"key":@"registBank",
               @"placeHolder" : @"请输入开户行名称",
               @"canEdit":@(YES),
               @"showAccess":@(NO),
               @"inputType":@(CHINESE),
               @"maxLength":@(30),
               @"sel":@"",
               @"height":self.type==1?@(0.00001):@(kComHeight)
           },
           @{
               @"title":@"银行账号",
               @"key":@"bankNo",
               @"placeHolder" : @"请输入银行账号",
               @"canEdit":@(YES),
               @"showAccess":@(NO),
               @"maxLength":@(30),
               @"keyboardType":@(UIKeyboardTypeNumberPad),
               @"inputType":@(NUM),
               @"sel":@"",
               @"height":self.type==1?@(0.00001):@(kComHeight)
           },
           @{
               @"title":@"备注",
               @"key":@"remarks",
               @"height":@(150)
           },

    ];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.data[indexPath.row];
    return [dic[@"height"] doubleValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary *dic = self.data[indexPath.row];
    //  备注
    if([dic[@"key"] isEqualToString:@"remarks"]){
       LXRemarkTabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
       [cell setClipsToBounds:YES];
       cell.textView.placeholder = @"请输入备注...";
       cell.textView.maxLength = 200;
       cell.remarkTitle.text = dic[@"title"];
       cell.textView.text = self.loanDic[dic[@"key"]];
       [cell setTextViewEdit:^(NSString * _Nonnull textStr) {
           self.loanDic[dic[@"key"]]  = textStr;
       }];
       return cell;
   }
    
    LXTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setClipsToBounds:YES];
    

    cell.cellDic = dic;
    cell.textFiled.hidden = NO;
    cell.subTitleLab.hidden = YES;
    cell.textFiled.text = self.loanDic[dic[@"key"]];
    

    cell.textfiledChangeBlock = ^(NSString * _Nonnull textStr) {
        self.loanDic[dic[@"key"]]  = textStr;
    };
    
    
    return cell;
}



- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kViewTopHeight, ScreenWidth, kScreenHeight - kViewTopHeight - kComHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = White_Color;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}


#pragma mark ---- 确认新增/修改点击事件
- (void)clickBottomView:(UIButton *)sender{
    
   
    if (StringIsEmpty(self.loanDic[@"name"])) {
        if (self.type == 1) {
            showAlert(@"请输入修理厂名称");
        }
        else {
            showAlert(@"请输入合作公司名称");
        }
        return;
    }
    
    if (StringIsEmpty(self.loanDic[@"address"])) {
        
        if (self.type == 1) {
            showAlert(@"请输入修理厂地址");
        }
        else {
            showAlert(@"请输入合作公司地址");
        }
        return;
    }
    
    if (StringIsEmpty(self.loanDic[@"contact"])) {
        showAlert(@"请输入联系人姓名");
        return;
    }
    
    if (StringIsEmpty(self.loanDic[@"contactPhone"])) {
        showAlert(@"请输入联系人手机号");
        return;
    }
    if (![self.loanDic[@"contactPhone"] isValidPhone]) {
        showAlert(@"请输入正确的联系人手机号");
        return;
    }
    if (self.type==1 && StringIsEmpty(self.loanDic[@"tel"])) {
        showAlert(@"请输入联系人电话");
        return;
    }
    
    if (!StringIsEmpty(self.loanDic[@"tel"])) {
        if ([self.loanDic[@"tel"] length] < 8 || [self.loanDic[@"tel"] length] > 11) {
            showAlert(@"联系人电话在8-11位数字");
            return;
        }
    }
    
    
    [Tool showAlertTitle:@"是否确认提交" msg:nil cancelTitle:@"取消" commitBtnTitle:@"确定" cancelHandlerBlock:^{
    } commitHandlerBlock:^{
        
        NSMutableDictionary *dic = @{
            
            @"type":@(self.type), // 公司
            @"remark":self.loanDic[@"remarks"],// 备注
            @"name":self.loanDic[@"name"],// 名称
            @"address":self.loanDic[@"address"],// 地址
            @"contact":self.loanDic[@"contact"],//  联系人
            @"contactPhone":self.loanDic[@"contactPhone"], //  联系人手机
            @"tel":self.loanDic[@"tel"], //  联系人电话
            @"registBank":self.loanDic[@"registBank"], //  开户行
            @"bankNo":self.loanDic[@"bankNo"], //  银行账号
           
        }.mutableCopy;
        
        
        
        [MSNetworkManager postRevertUrl:POST_PostThirdpartycompany params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
            
            if (isSuccess) {
                showAlert(@"新增成功");
                kUserDefaultSetBool(YES, kRefreshStr);
                [self.navigationController popViewControllerAnimated:YES];
            }
        } failure:^(NSUInteger code, NSString * _Nonnull message) {
        } withShowHud:YES];
    } onVC:self];
    
    
}

@end
